package com.github.llyb120.report

class All{
    private final static ThreadLocal<LinkedList> local = new ThreadLocal<LinkedList>(){
        @Override
        protected LinkedList initialValue() {
            return new LinkedList()
        }
    }

    static Report report(Closure closure){
        def _report = new Report()
        pushStack(_report)
        closure.call()
        popStack()
        return _report
    }

    static Col col(Closure closure){
        def _col = new Col()
        def item = pickStack()
        if(item instanceof Report){
            item.getCol().add(_col)
        }
        pushStack(_col)
        closure.call()
        popStack()
        return _col
    }

    static void datasource(String val){
        def item = pickStack()
        if(item instanceof Report){
            item.setDatasource(val)
        }
    }

    static void params(String val){
        def item = pickStack()
        if(item instanceof Report){
            item.setParams(val)
        }
    }

    static void calculate(Closure val){
        def item = pickStack()
        if(item instanceof Col){
            item.setCalculate(val)
        }
    }

    static void en(String val){
        def item = pickStack()
        if(item instanceof Col){
            item.setEn(val)
        }
    }

    static void cn(String val){
        def item = pickStack()
        if(item instanceof Col){
            item.setCn(val)
        }
    }

    private final static Object pickStack(){
        return local.get().getLast()
    }
    private final static Object pushStack(obj){
        local.get().addLast(obj)
    }
    private final static Object popStack(){
        return local.get().removeLast()
    }
}